﻿using System;
using System.Text;

namespace BinaryToHexadecimalDirectly
{
    class BinaryToHexadecimalDirectly
    {
        static string ToHexadecimal(string number)
        {
            StringBuilder result = new StringBuilder(); //Capacity for speed improve

            number = number.Replace(" ", ""); //remove all unnecessary spaces

            result.Append(number);
            while (result.Length % 4 != 0) //add zeros if needed
            {
                result.Insert(0, '0');
            }
            number = result.ToString();
            result.Clear();

            for (int index = 0, length = number.Length; index < length; index += 4)
            {
                switch (number.Substring(index, 4))
                {
                    case "1010": result.Append("A"); break;
                    case "1011": result.Append("B"); break;
                    case "1100": result.Append("C"); break;
                    case "1101": result.Append("D"); break;
                    case "1110": result.Append("E"); break;
                    case "1111": result.Append("F"); break;
                    case "0000": result.Append("0"); break;
                    case "0001": result.Append("1"); break;
                    case "0010": result.Append("2"); break;
                    case "0011": result.Append("3"); break;
                    case "0100": result.Append("4"); break;
                    case "0101": result.Append("5"); break;
                    case "0110": result.Append("6"); break;
                    case "0111": result.Append("7"); break;
                    case "1000": result.Append("8"); break;
                    case "1001": result.Append("9"); break;
                }
            }
            
            return result.ToString();
        }
        static void Main()
        {
            Console.Write("Enter a binary number: ");
            string number = Console.ReadLine();

            Console.WriteLine(ToHexadecimal(number));
        }
    }
}
